/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.agtranscontinental.sisref.sessionbean;

import com.agtranscontinental.dgs001.dto.OrdenDto;
import com.agtranscontinental.sisref.dto.SerieDto;
import com.agtranscontinental.sisref.model.Orden;
import com.agtranscontinental.sisref.model.Serie;
import com.agtranscontinental.sisref.sessionremote.OrdenEjbRemote;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author ericson
 */
@Stateless
public class OrdenEjbBean implements OrdenEjbRemote {

    @PersistenceContext(unitName = "sisref_pu")
    private EntityManager emSisref;

    @Override
    public OrdenDto getOrden(Integer anio, String rgmen, String adna, String odn) {
        try {
            Query q = emSisref.createNamedQuery("Orden.findOdn");
            q.setParameter("anio", anio);
            q.setParameter("rgmen", rgmen);
            q.setParameter("adna", adna);
            q.setParameter("odn", odn);
            Orden orden = (Orden) q.getSingleResult();
            if (orden != null) {
                return orden.toOrdenDto();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<SerieDto> getSerieDtoLst(Integer idOden) {
        List<SerieDto> serieLst = null;
        try {
            Query q = emSisref.createNamedQuery("Serie.findByOrden");
            q.setParameter("id", idOden);
            List<Serie> sLst = q.getResultList();
            if (sLst != null && !sLst.isEmpty()) {
                serieLst = new ArrayList<SerieDto>();
                for (Serie s : sLst) {
                    serieLst.add(s.toSerieDto());
                }
            }
        } catch (Exception e) {
        }
        return serieLst;
    }
}
